<?php

// ##############################################################################
// ASTPP - Open Source VoIP Billing Solution
//
// Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
// Samir Doshi <samir.doshi@inextrix.com>
// ASTPP Version 3.0 and above
// License https://www.gnu.org/licenses/agpl-3.0.html
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ##############################################################################
class Signup_model extends CI_Model {
	function Signup_model() {
		parent::__construct ();
		
		$this->load->model ( 'Astpp_common' );
		$this->load->helper ( 'form' );
		$this->load->model ( 'common_model' );
		$this->load->library ( 'session' );
	}
	function get_rate() {
		$data = array ();
		$this->load->database ();
		
		$this->db->select ( "id,name" );
		$this->db->from ( 'pricelists' );
		$this->db->where ( "status", "0" );
		$query = $this->db->get ();
		return $query->row ();
	}
	function add_user($data) {
		$data ['reseller_id'] = $data ['key_unique'];
		unset ( $data ['agreeCheck'] );
		unset ( $data ['key_unique'] );
		$data ['creation'] = gmdate ( 'Y-m-d H:i:s' );
		$data ['expiry'] = date ( 'Y-m-d H:i:s', strtotime ( '+10 years' ) );
		$this->db->insert ( "accounts", $data );
		return $this->db->insert_id ();
	}
	function check_user($accno, $email, $balance) {
		$info = array (
				"number" => $accno,
				"email" => $email,
				"status" => 1 
		);
		$this->db->where ( $info );
		$this->db->select ( '*' );
		$acc_res = $this->db->get ( 'accounts' );
		if ($acc_res->num_rows () > 0) {
			$acc_res = $acc_res->result_array ();
			$acc_res = $acc_res [0];
			$this->db->where ( 'pricelist_id', $acc_res ['pricelist_id'] );
			$this->db->select ( "*" );
			$charge_res = $this->db->get ( 'charges' );
			
			if ($charge_res->num_rows () > 0) {
				$charge_res = $charge_res->result_array ();
				$charge_res = $charge_res [0];
				$charge_acc_arr = array (
						"charge_id" => $charge_res ['id'],
						"accountid" => $acc_res ['id'],
						"status" => 0,
						"assign_date" => date ( 'Y-m-d H:i:s' ) 
				);
			} else {
				$charge_res = $charge_res->result_array ();
				// echo "<pre>"; print_r($charge_res); exit;
				$charge_acc_arr = array (
						"charge_id" => 'id',
						"accountid" => $acc_res ['id'],
						"assign_date" => date ( 'Y-m-d H:i:s' ) 
				);
			}
			$result = $this->db->insert ( "charge_to_account", $charge_acc_arr );
			$update = array (
					"status" => 0,
					"balance" => $balance 
			);
			$this->db->where ( $info );
			$result = $this->db->update ( 'accounts', $update );
			$sip_device_update = array (
					'username' => $accno,
					"status" => 1 
			);
			return 1;
		} else {
			return 0;
		}
	}
}

?>
